package com.sgq.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sgq.dao.RoleMapper;
import com.sgq.dao.UserMapper;
import com.sgq.entity.Department;
import com.sgq.dao.DepartmentMapper;
import com.sgq.entity.User;
import com.sgq.service.DepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sgq.utils.DepartmentTree;
import com.sgq.vo.DepartmentQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sgq
 * @since 2024-09-13
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public IPage<Department> findPage(DepartmentQueryVo departmentQueryVo) {
        //传递一个page对象 =》 当前页  数据条数大小
        System.out.println("departmentQueryVo = " + departmentQueryVo);
        Long pageNum = departmentQueryVo.getPageNum();
        Long pageSize = departmentQueryVo.getPageSize();
        IPage<Department> page = new Page<>(pageNum,pageSize);
        //条件按名字
        LambdaQueryWrapper<Department> queryWrapper = new LambdaQueryWrapper<>();
        //判断条件是否为空 "username"
        queryWrapper.like(!ObjectUtils.isEmpty(departmentQueryVo.getDepartmentName()), Department::getDepartmentName, departmentQueryVo.getDepartmentName());

        IPage<Department> iPage = this.baseMapper.selectPage(page, queryWrapper);
        return iPage;
    }

    /**
     * 根据条件查询部门列表
     * @param departmentQueryVo
     * @return
     */
    @Override
    public List<Department> findDepartmentList(DepartmentQueryVo departmentQueryVo) {

        LambdaQueryWrapper<Department> wrapper = new LambdaQueryWrapper<>();
        //通过名字去模糊查询
        wrapper.like(!ObjectUtils.isEmpty(departmentQueryVo.getDepartmentName()),Department::getDepartmentName,departmentQueryVo.getDepartmentName());

        wrapper.orderByAsc(Department::getOrderNum);

        List<Department> departments = this.baseMapper.selectList(wrapper);

        return departments;
    }

    /**
     * 查询所有部门列表
     * @return
     */
    @Override
    public List<Department> findParentDepartment() {
        //创建条件构造器对象
        QueryWrapper<Department> queryWrapper = new QueryWrapper<Department>();
        //排序
        queryWrapper.orderByAsc("order_num" );
        //查询部门列表
        List<Department> departmentList = baseMapper.selectList(queryWrapper);
        //创建部门对象
        Department department = new Department();
        department.setId(0L);
        department.setDepartmentName("顶级部门");
        department.setPid(-1L);
        departmentList.add(department);
        //生成部门菜单树
        List<Department> departmentTree = DepartmentTree.makeDepartmentTree(departmentList, -1L);
        return departmentTree;
    }

    /**
     * 判断是否存在子部门
     * @param id
     * @return
     */
    @Override
    public Boolean check(Long id) {
        LambdaQueryWrapper<Department> queryWrapper = new LambdaQueryWrapper<>();
        //是否存在把我的id作为父id的对象
        queryWrapper.eq(Department::getPid,id);
        Long count = this.baseMapper.selectCount(queryWrapper);
        return count > 0 ?  true : false;
    }

    /**
     * 判断部门下是否存在用户
     * @param id 部门id
     * @return
     */
    @Override
    public boolean hasUserInDepartment(Long id) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getDepartmentId,id);
        Long count = userMapper.selectCount(queryWrapper);
        return count > 0 ? true : false;
    }
}
